package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.CustomizeProductionDetailActivity;
import com.hemaapp.yjnh.activity.FarmerCustomOrderDetailActivity;
import com.hemaapp.yjnh.bean.FarmerCustomOrder;
import com.hemaapp.yjnh.fragment.FarmerCustomizeOrderListFragment;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.listener.OrderItemClickListener;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 田家的定制订单列表适配器
 *
 * @author GuiyouZhang
 */
public class FarmerCustomOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<FarmerCustomOrder> mDatas;
    private Context mContext;
    private OrderItemClickListener listener;

    public FarmerCustomOrderAdapter(Context context, ArrayList<FarmerCustomOrder> orders, OrderItemClickListener listener) {
        this.mContext = context;
        this.mDatas = orders;
        this.listener = listener;
    }

    public void setListener(OrderItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_custom_order, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas.size() == 0)
            return -1;
        else
            return 0;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (mDatas.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("暂时没有订单");
        }else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setDatas(position);
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mDatas.size() == 0 ? 1 : mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_mobile)
        TextView tvMobile;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.tv_order_date)
        TextView tvOrderDate;
        @Bind(R.id.layout_production)
        LinearLayout layoutProduction;
        @Bind(R.id.iv_goods)
        ImageView ivGoods;
        @Bind(R.id.tv_goods)
        TextView tvGoods;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_mu)
        TextView tvMu;
        @Bind(R.id.tv_delivery_date)
        TextView tvDeliveryDate;
        @Bind(R.id.tv_send)
        TextView tvSend;

        private int position = 0;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.onItemClickListener(position);
                }
            });
            tvSend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.onDeliveryGoods(position);
                }
            });
            layoutProduction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, CustomizeProductionDetailActivity.class);
                    intent.putExtra("blog_id", mDatas.get(position).getChildItems().get(0).getBlog_id());
                    mContext.startActivity(intent);
                }
            });
        }

        public void setDatas(int position) {
            this.position = position;
            tvName.setText(mDatas.get(position).getConsignee());
            tvMobile.setText(mDatas.get(position).getPhone());
            if (mDatas.get(position).getSendflag().equals("0")) {
                tvStatus.setText("未发货");
                tvSend.setVisibility(View.VISIBLE);
            }else {
                tvStatus.setText("已发货");
                tvSend.setVisibility(View.GONE);
            }
            tvOrderDate.setText(mDatas.get(position).getRegdate());
            ImageUtils.loadSmalImage(mContext, mDatas.get(position).getChildItems().get(0).getImgurl(), ivGoods);
            tvGoods.setText(mDatas.get(position).getChildItems().get(0).getName());
            tvPrice.setText("¥"+ uuUtils.formatAfterDot2(mDatas.get(position).getChildItems().get(0).getPrice()));
            tvMu.setText(mDatas.get(position).getChildItems().get(0).getBuycount()+mDatas.get(position).getChildItems().get(0).getGood_unit());
            tvDeliveryDate.setText(mDatas.get(position).getExpect_senddate());
        }
    }

}
